package per.chenxin.scale;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

import org.apache.commons.codec.binary.Base64;

public class Proxy implements Serializable {

    private static final long serialVersionUID = 1291060410984860920L;

    static Proxy proxy;

    boolean useInternalProxy = false;

    String internalProxyHost = "";

    int internalProxyPort = 0;

    boolean isAuthcation = false;

    String username = "";

    String password = "";

    boolean useExternalProxy = false;

    String externalProxyHost = "";

    int externalProxyPort = 0;

    public static Proxy getProxy() {

        return proxy;
    }

    public static Proxy getNewProxy() {

        return proxy;
    }

    private Proxy() {

    }

    public boolean isUseInternalProxy() {

        return useInternalProxy;
    }

    public void setUseInternalProxy(boolean useInternalProxy) {

        this.useInternalProxy = useInternalProxy;
    }

    public String getInternalProxyHost() {

        return internalProxyHost;
    }

    public void setInternalProxyHost(String internalProxyHost) {

        this.internalProxyHost = internalProxyHost;
    }

    public int getInternalProxyPort() {

        return internalProxyPort;
    }

    public void setInternalProxyPort(int internalProxyPort) {

        this.internalProxyPort = internalProxyPort;
    }

    public boolean isAuthcation() {

        return isAuthcation;
    }

    public void setAuthcation(boolean isAuthcation) {

        this.isAuthcation = isAuthcation;
    }

    public String getUsername() {

        return username;
    }

    public void setUsername(String username) {

        this.username = username;
    }

    public String getPassword() {

        return password;
    }

    public void setPassword(String password) {

        this.password = password;
    }

    public boolean isUseExternalProxy() {

        return useExternalProxy;
    }

    public void setUseExternalProxy(boolean useExternalProxy) {

        this.useExternalProxy = useExternalProxy;
    }

    public String getExternalProxyHost() {

        return externalProxyHost;
    }

    public void setExternalProxyHost(String externalProxyHost) {

        this.externalProxyHost = externalProxyHost;
    }

    public int getExternalProxyPort() {

        return externalProxyPort;
    }

    public void setExternalProxyPort(int externalProxyPort) {

        this.externalProxyPort = externalProxyPort;
    }

    public String getProxyAuthorization() {

        if (!isAuthcation)
            return null;

        return new String(Base64.encodeBase64((username + ":" + password).getBytes()));
    }

    static public Proxy inport() throws FileNotFoundException, IOException, ClassNotFoundException {

        ObjectInputStream ois = new ObjectInputStream(new FileInputStream(
            Proxy.class.getSimpleName()));
        try {
            return (Proxy) ois.readObject();
        } finally {
            ois.close();
        }

    }

    static public void export(Proxy proxy) throws FileNotFoundException, IOException {

        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(
            Proxy.class.getSimpleName()));
        try {
            oos.writeObject(proxy);
            oos.flush();
        } finally {
            oos.close();
        }

    }

    static {
        proxy = new Proxy();
        try {
            proxy = inport();
        } catch (FileNotFoundException e) {
        } catch (IOException e) {
        } catch (ClassNotFoundException e) {
        }

    }

}
